package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Student;
import com.situ.web.exception.NameAlreadyExistException;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;

public class AddStudentServlet extends HttpServlet{
	private IStudentService studentService = new StudentServiceImpl();
	
//	@Override
//	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		System.out.println("AddStudentServlet.doGet()");
//		doPost(req, resp);
//	}
//	
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		String name = req.getParameter("name");
//		String age = req.getParameter("age");
//		String gender = req.getParameter("gender");
//		Student student = new Student(name, Integer.parseInt(age), gender);
//		boolean result = false;
//		try {
//			result = studentService.regist(student);
//		} catch (NameAlreadyExistException e) {
//			e.printStackTrace();
//		}
//		resp.sendRedirect(req.getContextPath() + "/findAllStudent");
//	}
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		boolean result = false;
		try {
			result = studentService.regist(student);
		} catch (NameAlreadyExistException e) {
			e.printStackTrace();
		}
		
		
		/*resp.setContentType("text/html;charset=utf-8");
		PrintWriter printWriter = resp.getWriter();
		if (result) {
			printWriter.println("注册成功");
		} else {
			printWriter.print("注册失败");
		}
		printWriter.close();*/
		
		resp.sendRedirect(req.getContextPath() + "/findAllStudent");
	}
}
